package com.f6.common.util;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.ProtocolException;
import java.net.URI;
import java.net.URISyntaxException;
import java.net.URL;
import java.net.URLEncoder;

import net.sf.json.JSONObject;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.protocol.HTTP;
import org.apache.http.util.EntityUtils;

/**
 * @author songpeng
 * @date 2013-11-30
 **/

public class JsonUtil {

	public static JSONObject resolvejsonFromUrl(String jsonUrl) {
		URL url;
		String inputline = "";
		String info = "";
		JSONObject jsonob = null;
		try {
			url = new URL(jsonUrl);
			HttpURLConnection conn = (HttpURLConnection) url.openConnection();
			conn.setReadTimeout(10 * 1000);
			conn.setRequestMethod("GET");
			conn.setDoInput(true);  
            conn.setDoOutput(true); 
            conn.setInstanceFollowRedirects(true);  
            conn.setRequestProperty("content-type", "text/html");
            conn.setRequestProperty("Charsert", "UTF-8");
            
			InputStreamReader inStream = new InputStreamReader(
					conn.getInputStream(), "UTF-8");
			BufferedReader buffer = new BufferedReader(inStream);
			while ((inputline = buffer.readLine()) != null) {
				info += inputline;
			}
			jsonob = JSONObject.fromObject((JSONObject.fromObject(info)));
		} catch (ProtocolException e) {
			e.printStackTrace();
		} catch (MalformedURLException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
		return jsonob;
	}
	
	public static JSONObject resolvejsonFromUrlForParam(String jsonUrl) {
		JSONObject jsonob = null;
		DefaultHttpClient httpClient = httpClient = new DefaultHttpClient();
		httpClient
		.getParams()
		.setParameter("User-Agent",
				"Mozilla/5.0 (Windows NT 5.1; rv:15.0) Gecko/20100101 Firefox/15.0.1");
		HttpGet httpGet = null;
		// 响应信息。
		HttpResponse response = null;
		try {
			URL url = new URL(jsonUrl);
			System.out.println(url);
			URI uri = new URI(url.getProtocol(), url.getHost()+":"+url.getPort(), url.getPath(), url.getQuery(), null);
			
			httpGet = new HttpGet(uri);
			response = httpClient.execute(httpGet);
			String content = (EntityUtils.toString(response.getEntity()));
			jsonob = JSONObject.fromObject((JSONObject.fromObject(content)));
		} catch (ProtocolException e) {
			e.printStackTrace();
		} catch (MalformedURLException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		} catch (URISyntaxException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} finally{
			try {
				if(httpGet!=null){
					httpGet.abort();
				}
			}
			catch (Exception e) {
				
			}
		}
		return jsonob;
	}
	
	public static JSONObject cityNameFromUrl(String jsonUrl) {
		URL url;
		String inputline = "";
		String info = "";
		JSONObject jsonob = null;
		try {
			url = new URL(jsonUrl);
			HttpURLConnection conn = (HttpURLConnection) url.openConnection();
			conn.setReadTimeout(10 * 1000);
			conn.setRequestMethod("GET");
			InputStreamReader inStream = new InputStreamReader(
					conn.getInputStream(), "UTF-8");
			BufferedReader buffer = new BufferedReader(inStream);
			while ((inputline = buffer.readLine()) != null) {
				info += inputline;
			}
			jsonob = JSONObject.fromObject(info);
		} catch (ProtocolException e) {
			e.printStackTrace();
		} catch (MalformedURLException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
		return jsonob;
	}
	
	public static JSONObject resolvejsonFromUrlForParamPost(String url,String params) {
		JSONObject jsonob = null;
		HttpPost httpPost = null;
		// 响应信息。
		String content = "";
		try {
			String encoderJson = URLEncoder.encode(params, HTTP.UTF_8);
			
			httpPost = new HttpPost(url);
			httpPost.addHeader(HTTP.CONTENT_TYPE, "application/json");
			StringEntity se = new StringEntity(encoderJson);
			se.setContentType("application/json");
			se.setContentEncoding("UTF-8");
	        httpPost.setEntity(se);
	        
            HttpResponse httpResponse = new DefaultHttpClient().execute(httpPost);  
            if(httpResponse.getStatusLine().getStatusCode() == 200)  
            {  
                HttpEntity httpEntity = httpResponse.getEntity();  
                content = (EntityUtils.toString(httpEntity));
    			jsonob = JSONObject.fromObject((JSONObject.fromObject(content)));
            }  
		} catch (ProtocolException e) {
			e.printStackTrace();
		} catch (MalformedURLException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		} finally{
			try {
				if(httpPost!=null){
					httpPost.abort();
				}
			}
			catch (Exception e) {
				
			}
		}
		return jsonob;
	}

}
